package com.financing.wap.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dao.IManagerOrderDao;
import com.financing.wap.dao.IMessageDao;
import com.financing.wap.dao.IOrderDao;
import com.financing.wap.dao.IOrderTmpDao;
import com.financing.wap.dao.ISystemDao;
import com.financing.wap.dto.ManagerOrderDTO;
import com.financing.wap.dto.ManagerOrderHistoryDTO;
import com.financing.wap.dto.OrderTmpDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IOrderTmpService;
import com.financing.wap.service.IUserService;
import com.financing.wap.util.Constants;

@Service("orderTmpServiceImpl")
public class OrderTmpServiceImpl implements IOrderTmpService{

	@Autowired
	private IOrderTmpDao orderTmpDaoImpl;
	@Autowired
	private IOrderDao orderDao;
	@Autowired
	private IManagerOrderDao managerOrderDao;
	@Autowired
	private IUserService userService;
	
	
	@Override
	public String addOrderTmp(OrderTmpDTO dto) {
		String count = orderTmpDaoImpl.addOrderTmp(dto);
		if("02".equals(dto.getoType())){
			addManagerUser(dto);
		}
		return count;
	}

	public void addManagerUser(OrderTmpDTO order){
		List<String> list = orderDao.countOrderManagerForUserId(order.getFaId());
		if(list != null && list.size() == 1){
			doAddManagerUser(order, list.get(0));
		}else {
			Session session = SessionUtil.getSession(false);
			UserDTO user = new UserDTO(); 
			user = session.getUser();
			if(user==null || StringUtils.isBlank(user.getLoginName())){
				UserDTO u = new UserDTO();
				u.setLoginName(order.getUpdateBy());
				user = userService.queryUserLoginName(u);
			}
			
			if(user.getSourceCode() != null && !("").equals(user.getSourceCode()) && !("0").equals(user.getSourceCode())){
				// 查询登录用户对应的邀请人是否是订单管理员
				List<String> sysList = orderDao.queryOrderManagerUserBySource(user.getSourceCode());
				if(sysList != null && sysList.size() > 0){
					doAddManagerUser(order, sysList.get(0));
				}else{
					doAddManagerUser(order, "9253fc40-f853-11e4-aa1d-00163e0e2df9");
				}
			}else{
				doAddManagerUser(order, "9253fc40-f853-11e4-aa1d-00163e0e2df9");
			}
		}
	}
	
	public void doAddManagerUser(OrderTmpDTO order, String systemUserId) {
		// 根据产品Code查询产品信息
		ManagerOrderDTO mo = new ManagerOrderDTO();
		mo.setOrderId(order.getId());
		mo.setOrderType(Constants.simuOrderType);
		mo.setProjectId(order.getProductId());
		mo.setProjectType(Constants.simuProjectType);
		mo.setFaUserId(order.getFaId());
		mo.setSystemUserId(systemUserId);
		mo.setCreateBy(order.getCreateBy());
		mo.setUpdateBy(order.getCreateBy());
		managerOrderDao.addManagerOrderUser(mo);
		// 历史
		ManagerOrderHistoryDTO managerOrderHistory = new ManagerOrderHistoryDTO();
		managerOrderHistory.setOrderId(mo.getOrderId());
		managerOrderHistory.setOrderType(mo.getOrderType());
		managerOrderHistory.setProjectId(mo.getProjectId());
		managerOrderHistory.setProjectType(mo.getProjectType());
		managerOrderHistory.setFaUserId(mo.getFaUserId());
		managerOrderHistory.setSystemUserIdFrom("");
		managerOrderHistory.setSystemUserIdTo(mo.getSystemUserId());
		managerOrderHistory.setCreateBy(mo.getCreateBy());
		managerOrderHistory.setCreateDate(mo.getCreateDate());
		managerOrderHistory.setUpdateBy(mo.getUpdateBy());
		managerOrderHistory.setUpdateDate(mo.getUpdateDate());
		managerOrderDao.addManagerOrderUserHistory(managerOrderHistory);// ADD
		
	}
}
